package com.itheima.health.controller;

import com.itheima.interfaces.SetmealService;
import com.itheima.pojo.Setmeal;
import com.itheima.resources.MessageConstant;
import com.itheima.resources.RedisConstant;
import com.itheima.resources.UploadUtils;
import com.itheima.resources.aliyunoss.AliyunUtils;
import com.itheima.resources.entity.QueryPageBean;
import com.itheima.resources.entity.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;  //   操作  Set
    @Reference
    private SetmealService setmealService;

    //  套餐预约占比
    @RequestMapping("getSetmealZhanBi.do")
//    @PreAuthorize()
    public Result getSetmealZhanBi() {
        try {
            List<Map> map = setmealService.getSetmealZhanBi();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }

    @RequestMapping("upload.do")
    public Result upload(@RequestParam("imgFile") MultipartFile file, HttpSession session) {
        try {
            System.out.println("---------------收到上传了---");
            //  文件上oss上 并且uudi名称 回送给前端
            String originalFilename = file.getOriginalFilename();//获取文件名
            String uuid = UploadUtils.generateRandonFileName(originalFilename);
            //   文件上传到oss
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(), uuid);
            // 上传好阿里云图片之后 我们将图片存储到redis set 集合中
            //  将上传到阿里云的图片文件 保存在redis中
            //   文件存储redis   set 中
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES, uuid);//  文件添加到redis缓存中

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, uuid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //套餐的添加
    @RequestMapping("add.do")
    public Result add(@RequestBody Setmeal setmeal, @RequestParam("checkgroupIds") Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_SETMEAL_FAIL);
        }

    }

    //  有条件和无条件查询 都是同一个方法
    @RequestMapping("findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            Map map = setmealService.findPageByContion(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }
}
